//
//  RadioDetailViewController.m
//  Slice
//
//  Created by lanou on 16/6/20.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "RadioDetailViewController.h"
#import "DownLoaderTableViewController.h"
@interface RadioDetailViewController ()<MusicManagerDelegate>

@end

@implementation RadioDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
   self.playView = [[PlayDetailView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100) musicList:self.musicList];
    [self.view addSubview:self.playView];
    
    //显示当前正在播放的model
    [self.playView setCurrentModel:self.musicList[self.selectIndex]];
    
//    加载底部视图
    self.musicView =  [[[NSBundle mainBundle] loadNibNamed:@"MusicPlayView" owner:self options:nil] lastObject];
    [self.view addSubview:self.musicView];
    
    [self.musicView.nextBtn addTarget:self action:@selector(nextmusicBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self.musicView.previousBtn addTarget:self action:@selector(previousmusicBtn) forControlEvents:UIControlEventTouchUpInside];

    
    [self.musicView.startAndPauseBtn addTarget:self action:@selector(startAndPausemusicBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self.musicView.progressBtn addTarget:self action:@selector(progressSlider:) forControlEvents:UIControlEventValueChanged];
    
    [MusicManager share].delgate = self;
    
    UIView *view = [self.musicView viewWithTag:100];
    
    view.backgroundColor = [UIColor redColor];
    
    __block RadioDetailViewController *customerSelf = self;
    
//    通过block 传过来的当前值
    self.playView.sendValue = ^(CGFloat offsetX) {
        NSInteger index = offsetX / customerSelf.view.frame.size.width;

        for (int i = 100; i < 103; i++) {
            UIView *view = [customerSelf.musicView viewWithTag:i];
            if ((i - 100) == index) {
                view.backgroundColor = [UIColor redColor];
            } else {
                view.backgroundColor = [UIColor blueColor];
            }
        }
    };
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"下载界面" style:UIBarButtonItemStylePlain target:self action:@selector(click)];
    
    self.navigationItem.rightBarButtonItem = item3;
}
//下一曲动作
- (void) nextmusicBtn {
    if (self.selectIndex == self.musicList.count - 1) {
        self.selectIndex = 0;
    } else {
        self.selectIndex += 1;
    }
//    取出下一个model
    RadioDetailModel *model = self.musicList[self.selectIndex];
    [[MusicManager share] playMusicWithStringURL:model.musicUrl];
//    刷新界面
    [self.playView setCurrentModel:model];
}
//上一曲
- (void)previousmusicBtn {
    if (self.selectIndex == 0) {
        self.selectIndex = self.musicList.count - 1;
    } else {
        self.selectIndex -= 1;
    }
    //    取出上一个model
    RadioDetailModel *model = self.musicList[self.selectIndex];
    [[MusicManager share] playMusicWithStringURL:model.musicUrl];
    //    刷新界面
    [self.playView setCurrentModel:model];
}
//暂停开启
- (void)startAndPausemusicBtn {
    if ([MusicManager share].isPlaying) {
        [[MusicManager share] pause12];
        [self.musicView.startAndPauseBtn setImage:[UIImage imageNamed:@"player_play@2x"] forState:UIControlStateNormal];
    } else {
        [[MusicManager share] play12];
         [self.musicView.startAndPauseBtn setImage:[UIImage imageNamed:@"player_pause@2x"] forState:UIControlStateNormal];
    }
}
//进度条的方法
- (void)progressSlider:(UISlider *)slider {
    [[MusicManager share].player seekToTime:CMTimeMake(self.musicView.progressBtn.value ,1)];
    
}

- (void)currentTime:(CGFloat)currentTime totalTime:(CGFloat)totalTime {
    self.musicView.progressBtn.maximumValue = totalTime;
    
    self.musicView.progressBtn.value = currentTime;
    
}
//调转到下一个控制器
- (void) click {
    DownLoaderTableViewController *vc = [[DownLoaderTableViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}


@end
